﻿using Microsoft.ManagementConsole;

namespace Google.Apps.MMC.Root
    {
    class RootNode : ScopeNode
        {

        public RootNode()
            {
            this.ImageIndex = GoogleAppsSnapIn.icon_googleappsicon;
            this.SelectedImageIndex = GoogleAppsSnapIn.icon_googleappsicon;
            this.EnabledStandardVerbs = StandardVerbs.Properties;
            this.ActionsPaneItems.Add(new Action("Logout", "Logout of the current Google Apps Manager session and enter new credentials.", GoogleAppsSnapIn.icon_key));
            }

        protected override void OnAddPropertyPages(PropertyPageCollection propertyPageCollection)
            {
                GoogleAppsSnapIn snapin = (GoogleAppsSnapIn) this.SnapIn;
                PropertyPage page = new PropertyPage();

                page.Title = "Google Apps";

                page.Control = new RootNodePropertyPage(snapin.domain, snapin.username);
                propertyPageCollection.Add(page);
            }

        protected override void OnAction(Action action, AsyncStatus status)
            {
            base.OnAction(action, status);
            if (action.DisplayName == "Logout")
                {
                Logout();
                }
            }

        internal void Logout()
            {
            GoogleAppsSnapIn snapin = (GoogleAppsSnapIn) this.SnapIn;
            if (snapin.InitilizationWizard(false))
                {
                this.Children.Clear();
                snapin.InitializeNodes();
                }
            }
            
        }
    }
